package no.monsen.service;

import java.rmi.RemoteException;
import java.util.Properties;

import no.monsen.client.common.MonsenClientConstants;
import no.monsen.client.common.exception.MonsenException;
import no.monsen.client.common.factory.MonsenPropertiesFactory;
import no.monsen.client.common.logging.Logger;
import no.monsen.client.common.util.MonsenClientID;
import no.monsen.webservice.MonsenServiceSOAP11BindingStub;
import no.monsen.webservice.commons.xsd.MonsenFact;

import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Marius Breivik Created: 21.47.07 / 10. mars. 2008
 */
public final class MonsenService {

    @Autowired
    private MonsenPropertiesFactory propFactory = null;

    @Logger
    private Log log;
    
    @Autowired
    private MonsenServiceSOAP11BindingStub stub;

    private MonsenClientID id;
    
    @Autowired
	public MonsenService(MonsenClientID monsenClientID) {
    	id = monsenClientID;
	}
    /**
     * @param version # of the client
     * @return The newest version# available from server
     */
    public String isUpdateAvailable(String currentVersion) throws MonsenException {
        try {
            log.debug("Checking for update");
            return stub.isUpdateAvailable(currentVersion);
        } catch (RemoteException e) {
            log.error("Failed to retrieve connection to monsen-service" + e);
            throw new MonsenException(propFactory.getProperty(MonsenClientConstants.MONSEN_EXCEPTION_DB_CONNECTION_ERROR));
        }
    }

    /**
     * @return The message retrieved from the central web service
     */
    public MonsenFact getMonsenFact() throws MonsenException {
        MonsenFact monsenFact = new MonsenFact();
        try {
            monsenFact.setClientID( id.getUniqueClientID().toString());
            log.debug("Retriving a fact from monsen-service with the unique id : " + id.getUniqueClientID());
            monsenFact = stub.getMonsenFact(monsenFact);
            return monsenFact;
        } catch (RemoteException e) {
            log.error("Failed to retrieve connection to monsen-service" + e);
            throw new MonsenException(propFactory.getProperty(MonsenClientConstants.MONSEN_EXCEPTION_DB_CONNECTION_ERROR));
        }
    }
}
